package com.ebeitech.library.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by alice on 2017/10/24.
 */
public class SPManager {

	private static final String SP_NAME = "sys_setting";
	private SharedPreferences sp;
	private SharedPreferences.Editor editor;
	private Context mContext = null;

	private static SPManager manager = null;

	private SPManager(Context context) {
		mContext = context.getApplicationContext();
		sp = mContext.getSharedPreferences(SP_NAME, Activity.MODE_PRIVATE);
		editor = sp.edit();
	}

	public static SPManager getInstance(Context context) {
		if (manager == null) {
			manager = new SPManager(context);
		}
		return manager;
	}

	public void put(String key, String value) {
		editor.putString(key, value);
		editor.commit();
	}

	public void put(String key, int value) {
		editor.putInt(key, value);
		editor.commit();
	}

	public void put(String key, long value) {
		editor.putLong(key, value);
		editor.commit();
	}

	public void put(String key, boolean value) {

		editor.putBoolean(key, value);
		editor.commit();
	}

	public String get(String key, String defValue) {
		return sp.getString(key, defValue);
	}

	public int get(String key, int defValue) {
		return sp.getInt(key, defValue);
	}

	public long get(String key, long defValue) {
		return sp.getLong(key, defValue);
	}

	public boolean get(String key, boolean defValue) {
		return sp.getBoolean(key, defValue);
	}

	public void remove(String key) {
		editor.remove(key);
		editor.commit();
	}

	public void removeKeyAndValue(String key) {
		if (!StringUtil.isStringNullOrEmpty(key)) {
			if (sp.contains(key)) {
				editor.remove(key).commit();
			}
		}
	}

}
